﻿using BLL;
using DevExpress.XtraEditors;
using Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 后整理管理系统.自定义类;
using 后整理管理系统.选择窗体;
using 纺织贸易管理系统;
using 纺织贸易管理系统.其他窗体;
using 纺织贸易管理系统.设置窗体;
using 纺织贸易管理系统.选择窗体;

namespace 后整理管理系统.新增窗体
{
    public partial class 检测单 : Sunny.UI.UIForm 
    {
        private FormUseful Useful;
        private SqlSugarClient dbcontext = Connect.dbcontext;
        private TestData OldDoc = new TestData();
        public 检测单(FormUseful formUseful,TestData doc)
        {
            InitializeComponent();
            this.Useful = formUseful;
            this.OldDoc = doc;
            CreateGrid.Create("流转卡", gridView2, "gridView2");
        }

        private void 检测单_Load(object sender, EventArgs e)
        {
            if (Useful == FormUseful.新增) 
            {
                Addnew();
            }
            else
            {
                if (Useful == FormUseful.修改)
                {
                    Edit(OldDoc);
                }
                else
                {
                    if(Useful==FormUseful.复制)
                    {
                        Edit(OldDoc);
                        dateTimePicker1.Value = DateTime.Now;
                        txtdanhao.Text = BianhaoBLL.CreatDanhao(FirstLetter.检测单, dateTimePicker1.Value, DanjuLeiXing.检测单);
                    }
                }
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        private void Addnew()
        {
            dateTimePicker1.Value=DateTime.Now;
            txtdanhao.Text = BianhaoBLL.CreatDanhao(FirstLetter.检测单, dateTimePicker1.Value , DanjuLeiXing.检测单); 
            cmb测试类型.SelectedIndex = 0;
            txtordernum.Text = string.Empty;
            txtboli.Text = string.Empty;
            txtpingming.Text = string.Empty;
            txtshuiya.Text = string.Empty;
            txttoushi.Text = string.Empty;
            txtzhibiao.Text = string.Empty;
            txtkehu.Text = string.Empty;
            txt生成单号.Text = string.Empty;
            txt工艺名称.Text = string.Empty;
            txt水洗.Text = string.Empty;
            txt撕裂强度.Text =string.Empty;
            txt接缝滑移.Text = string.Empty;
            txt成品克重.Text = string.Empty;
            gridControl2.DataSource = null;
        }
        private void Edit(TestData doc)
        {
            txtdanhao.Text = doc.DocNum;
            cmb测试类型.Text = doc.TestType;
            txtpingming.Text = doc.FabricName;
            txttoushi.Text = doc.MoisturePermeability;
            txtshuiya.Text = doc.Hydraulic;
            txtzhibiao.Text = doc.Reqiured;
            txtboli.Text = doc.Divest;
            txtkehu.Text = doc.CustomerName;
            dateTimePicker1.Value = doc.DocumentDate;
            txtordernum.Text = doc.OrderNum;
            txt生成单号.Text = doc.ProductionOrderNo;
            txt工艺名称.Text = doc.Reqiured;
            txt水洗.Text = doc.Washing;
            txt撕裂强度.Text = doc.TearStrength;
            txt接缝滑移.Text = doc.SeamSlippage;
            txt成品克重.Text = doc.Weight;
            gridControl2.DataSource = dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == doc.DocNum).ToList();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            var linkman = new LXR() { ZJC = "", MC = "" };
            var fm = new 客户选择() { linkman = linkman };
            fm.ShowDialog();
            txtkehu.Text = fm.linkman.MC;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(txtkehu.Text))
            {
                AlterDlg.Show("请先输入客户");
                return;
            }
            if (string.IsNullOrEmpty(txt生成单号.Text))
            {
                AlterDlg.Show("请先选择一下流转卡号");
                return;
            }
            var items = gridControl2.DataSource as List<danjumingxitable>;
            if (items.Count==0)
            {
                AlterDlg.Show("请先选择一下布料");
                return;
            }
            if (string.IsNullOrEmpty(txtboli.Text)&&string.IsNullOrEmpty(txtshuiya.Text )&&string.IsNullOrEmpty(txtzhibiao.Text )&&string.IsNullOrEmpty(txttoushi.Text ))
            {
                AlterDlg.Show("请输入一个检测结果");
                return;
            }
            try
            {
                if (Useful == FormUseful.新增 || Useful == FormUseful.复制)
                {
                    var doc = new TestData();
                    AssignmentTestDoc(doc);
                    using (var trans = dbcontext .UseTran())
                    {
                        dbcontext.Insertable(doc).ExecuteCommand();
                        dbcontext.Insertable(gridControl2.DataSource as List<danjumingxitable>).ExecuteCommand();
                        dbcontext.CommitTran();
                    }
                }
                else
                {
                    if (Useful == FormUseful.修改)
                    {
                        AssignmentTestDoc(OldDoc);
                        using (var trans = dbcontext.UseTran())
                        {
                            dbcontext.Updateable(OldDoc).ExecuteCommand();
                            dbcontext.Deleteable<danjumingxitable>().Where(x => x.danhao == OldDoc.DocNum).ExecuteCommand();
                            dbcontext.Insertable(gridControl2.DataSource as List<danjumingxitable>).ExecuteCommand();
                            dbcontext.CommitTran();
                        }
                    }

                }
                AlterDlg.Show("保存成功");
                Addnew();
                Useful = FormUseful.新增;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("保存失败:"+ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void AssignmentTestDoc(TestData doc)
        {
            doc.DocNum = txtdanhao.Text;
            doc.FabricName = txtpingming.Text;
            doc.MoisturePermeability = txttoushi.Text;
            doc.Hydraulic = txtshuiya.Text;
            doc.Reqiured = txt工艺名称.Text;
            doc.Divest = txtboli.Text;
            doc.CustomerName = txtkehu.Text;
            doc.DocumentDate = dateTimePicker1.Value;
            doc.OrderNum = txtordernum.Text;
            doc.ProductionOrderNo = txt生成单号.Text;
            doc.TestType = cmb测试类型.Text;
            doc.Weight = txt成品克重.Text ;
            doc.SeamSlippage = txt接缝滑移.Text;
            doc.TearStrength = txt撕裂强度 .Text;
            doc.WaterproofEffect = txt防水效果 .Text;
            doc.Washing=txt水洗.Text;
        }

        private void txt生成单号_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SelectDetails();
            }
        }
        private void SelectDetails()
        {
            if (string.IsNullOrEmpty(txt生成单号.Text))
            {
                ShowMessageBox.ShowErrorBox("请输入生产单号");
                return;
            }
            var items = dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == txt生成单号.Text).ToList();
            if (items.Count == 0)
            {
                ShowMessageBox.ShowErrorBox("未找到对应的单号");
                return;
            }
            var doc = dbcontext.Queryable<Liuzhuancard>().First(x => x.CardNum == txt生成单号.Text);
            if (doc != null)
            {
                txtkehu.Text = doc.Customer;
               txt工艺名称.Text = doc.Beizhu;
            } 
            var fm=new 生成单明细选择 (items);
            fm.ShowDialog();
            if (fm.SelectItems.Count > 0)
            {
                foreach (var item in fm.SelectItems)
                {
                    item.danhao = txtdanhao.Text;
                }
                gridControl2.DataSource = fm.SelectItems;
            }
        }

        private void btn流转卡_Click(object sender, EventArgs e)
        {
            SelectDetails();
        }
    }
}
